package com.cj.cms.web.security;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.cj.cms.entity.Article;
import com.cj.cms.entity.Channel;
import com.cj.cms.service.ArticleManager;
import com.cj.cms.service.ChannelManager;
import com.cj.cms.template.PublishChannel;
import com.cj.common.orm.Page;
import com.cj.common.orm.PropertyFilter;
import com.cj.common.orm.PropertyFilter.MatchType;
import com.cj.common.orm.hibernate.EntityManager;
import com.cj.common.orm.hibernate.HibernateWebUtils;
import com.cj.common.web.struts2.CRUDActionSupport;
import com.cj.common.web.struts2.Struts2Utils;

public class ArticleAction extends CRUDActionSupport<Article>{
	@Autowired
	private ArticleManager articleManager;
	@Autowired
	private ChannelManager channelManager;
	protected Page<Article> page = new Page<Article>(25,"isTop,modifyTime",Page.DESC + "," + Page.DESC);
	@Autowired
	private PublishChannel publishChannel;

	@Override
	public EntityManager getEntityManager() {
		return articleManager;
	}
	
	private Long channelId;
	@Override
	public String save() {
		try {
			articleManager.save(entity);
			addActionMessage("保存成功");
			publishChannel.publishChannel(entity.getChannel());
			channelId = entity.getChannel().getId();
			return list();			
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	@Override
	public String list() {
		try {
			HttpServletRequest request = Struts2Utils.getRequest();
			List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(request);
			if(channelId != null)
				filters.add(new PropertyFilter("channel.id",channelId,MatchType.EQ));
			page = getEntityManager().search(page, filters);
			return SUCCESS;
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	
	/**
	 * 左边带树的article  list
	 * @return
	 */
	public String treeList(){
		try {
			return "treeList";
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	@Override
	public String input() {
		try {
			return INPUT;
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	public void prepareSave() throws Exception {
		if (getId() != null) {
			entity = articleManager.get(getId());
			entity.setChannel(null);
		} else {
			entity = new Article();
		}
	}
	
	protected void prepareModel() throws Exception{
		if (getId() != null) {
			entity = articleManager.get(getId());
		} else {
			entity = new Article();
			if(channelId != null){
				Channel channel  = channelManager.load(channelId);
				entity.setChannel(channel);
				entity.setContent(channel.getContent());
			}
		}
	}

	@Override
	public String remove() {
		try{
			return super.delete();
		}finally{
			if(channelId != null){
				Channel channel = channelManager.get(channelId);
				publishChannel.publishChannel(channel);
			}
		}
	}
	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}
	public Page<Article> getPage() {
		return page;
	}
	public void setPage(Page<Article> page) {
		this.page = page;
	}

}
